<template>
  <view>
    <view class="search-box">
      <uni-search-bar @input="input" radius="100" cancelButton="none" focus="true" placeholder="请输入搜索内容" />
    </view>
    <!-- 搜索建议 -->
    <view class="sugg-list" v-if="suggList.length!==0">
      <view class="sugg-item" v-for="(item,i) in suggList" :key="i" @click="gotoGoodsDetails(item)">
        <view class="sugg-text">{{item.goods_name}}</view>
        <uni-icons type="right" size="18"></uni-icons>
      </view>
    </view>
    <!-- 搜索历史 -->
    <view class="history-box" v-else>
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="clearHistory"></uni-icons>
      </view>
      <view class="history-list">
        <uni-tag :text="item" v-for="(item,i) in historyList" :key="i" @click="gotoGoodsList(item)"></uni-tag>

      </view>

    </view>

  </view>
</template>

<script>
  export default {
    data() {
      return {
        timeId: null,
        kw: '',
        //搜索建议
        suggList: [],
        //搜素历史
        historyList: [],
      };
    },
    onLoad() {
      this.historyList=JSON.parse(uni.getStorageSync('historyList')||[]); 
    },
    methods: {
      input(e) {
        //防止抖动效果
        clearTimeout(this.timeId);
        this.timeId = setTimeout(() => {
          //500ms没有触发新的输入事件再进行赋值处理
          this.kw = e;
          //去除左右两端空格
          if (this.kw.trim() !== '') {
            this.getSuggList();
          }else{
            this.suggList=[];
          }
        }, 500);
      },
      async getSuggList() {
        let {
          data
        } = await uni.$http.get('/api/public/v1/goods/qsearch?query=' + this.kw);
        //console.log(data);
        if (data.meta.status === 200) {
          this.suggList = data.message;
          this.saveSearchHistory();//查询到搜索记录后，再保存历史
          uni.setStorageSync('historyList',JSON.stringify(this.historyList) );//储存到本地
        } else {
          uni.$showMsg();
        }
      },
      gotoGoodsDetails(item) {
        uni.navigateTo({
          url: '/subpkg/goods_details/goods_details?goods_id=' + item.goods_id
        })
      },
      saveSearchHistory(){
      this.historyList.unshift(this.kw);
      let newSet=[...new Set(this.historyList)];
       this.historyList=newSet;
        
      },
      clearHistory(){
        this.historyList=[];
        uni.setStorageSync('historyList','[]');
      },
      gotoGoodsList(item){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?query='+item
        })
      }
    }
  }
</script>

<style lang="scss">
  .search-box {
    height: 50px;
    background-color: #c00000;
    position: sticky;
  }

  .sugg-list {
    padding: 0 5px;

    .sugg-item {
      font-size: 12px;
      padding: 13px 0;
      border-bottom: 1px solid #efefef;
      display: flex;

      // justify-content: center;
      // align-items: center;
      .sugg-text {
        // 文字不允许换行（单行文本）
        white-space: nowrap;
        // 溢出部分隐藏
        overflow: hidden;
        // 文本溢出后，使用 ... 代替
        text-overflow: ellipsis;
        margin-right: 3px;
      }

    }

  }

  .history-box {
    padding: 0 5px;

    .history-title {
      font-size: 14px;
      height: 40px;
      border-bottom: 1px solid #efefef;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }

    .history-list {

      // display: flex;
      // flex-wrap: wrap;
      .uni-tag {
        display: inline-block;
        margin-top: 5px;
        margin-right: 5px;
      }
    }
  }
</style>
